const express = require('express');
const { response } = require('../app');
const router = express.Router();
const Database = require('../model/connectDatabase');
const {SuccessModel,ErrorModel} = require('../model/resData');

//接受的参数title,describeStr,audioUrl,videoUrl,username
router.post('/',function(req, res, next) {
  let database = new Database().mysql;
  let username = req.body.username
  let sql = 'insert into resources(userId,title,describeStr,audioUrl,videoUrl,createTime) values(?,?,?,?,?,?)'
  database.query('select id from users where(username=?)',[username],(err,data)=>{
    // console.log(data[0].id)
    // res.send(data[0])
    let {title,describeStr,audioUrl,videoUrl} = req.body
    let userid = data[0].id
    let msg = [userid,title,describeStr,audioUrl,videoUrl,new Date()]
    database.query(sql,msg,(error,response)=>{
      if(response.affectedRows != 0){
        res.send(new SuccessModel(response,'新建成功'))
      }else{
        res.send(new ErrorModel(response,'新建失败'))
      }
      if(error){
        res.send(new ErrorModel(err,'数据库访问失败'))
      }
    })
  })
  
  
  // database.query(sql,msg,(err,data)=>{
  //   // console.log(data)
  //   if(err){
  //     res.send(new ErrorModel(err,'数据库访问错误'))
  //   }else{
  //     res.send(new SuccessModel(data,'请求成功'))
  //   }
  // });
});

module.exports = router;
